using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Robot.Business;
using Robot.Data;
using Robot.Entities;
using Scb.Framework;
using WebRobot.App_code;

namespace WebRobot.Pages
{
  public partial class ListFileUploaded : BasePage
  {
    #region Event

    protected void Page_Load(object sender, EventArgs e)
    {
      if (dateFrom.Value == null)
      {
        dateFrom.Value = DateTime.Today;
      }
      if (dateTo.Value == null)
      {
        dateTo.Value = DateTime.Now;
      }
      try
      {
        if (dateFrom.Value != null && dateTo.Value != null)
        {
          DateTime from = DateTime.Parse(dateFrom.Value.ToString());
          DateTime to = DateTime.Parse(dateTo.Value.ToString());

          LoadDataToGrid(from, to);

        }
        LoadWithID();


      }
      catch (Exception ex)
      {
        Logger.Error("WebRobot.Pages.ListFileUploaded::Page_Load", ex);
      }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
      try
      {
        if (dateFrom.Value != null && dateTo.Value != null)
        {
          DateTime from = DateTime.Parse(dateFrom.Value.ToString());
          DateTime to = DateTime.Parse(dateTo.Value.ToString());

          LoadDataToGrid(from, to);
          gvViewTran.DataSource = null;
          gvViewTran.DataBind();
        }
      }
      catch (Exception ex)
      {
        Logger.Error("WebRobot.Pages.ListFileUploaded::btnSearch_Click", ex);
      }

    }

    protected void gvFileUploaded_PageIndexChanged(object sender, EventArgs e)
    {
      try
      {
        if (dateFrom.Value != null && dateTo.Value != null)
        {
          DateTime from = DateTime.Parse(dateFrom.Value.ToString());
          DateTime to = DateTime.Parse(dateTo.Value.ToString());

          LoadDataToGrid(from, to);

        }
      }
      catch (Exception ex)
      {
        Logger.Error("WebRobot.Pages.ListFileUploaded::gvFileUploaded_PageIndexChanged", ex);
      }
    }

    #endregion

    #region Helper

    private tblFileUploadCollection GetPDFListFile(DateTime from, DateTime to, string peoplewise)
    {
      tblFileUploadCollection fColection = tblFileUploadService.GetPDFFileByCreatedDate(from, to, peoplewise);
      return fColection;
    }

    private void LoadDataToGrid(DateTime from, DateTime to)
    {
      gvFileUploaded.DataSource = GetPDFListFile(from, to, CurrentUser.Peoplewise);
      gvFileUploaded.DataBind();
      gvFileUploaded.Visible = true;
    }

    private void LoadWithID()
    {
      try
      {
        if (Request.QueryString["id"] != null)
        {
          string sID = Request.QueryString["id"];
          Guid id = new Guid(sID);
          tblFileUploadCollection list = new tblFileUploadCollection();
          list.Add(tblFileUploadService.GettblFileUpload(id));
          gvFileUploaded.DataSource = list;
          gvFileUploaded.DataBind();

          gvViewTran.DataSource = tblImportTransactionService.GettblImportTransactionByFileID(id);
          gvViewTran.DataBind();
        }

      }
      catch (Exception ex)
      {
        Logger.Error("WebRobot.Pages.ViewTransaction::Page_Load", ex);
        Response.Redirect("UploadedData.aspx");
      }
    }

    protected void gvViewTran_PageIndexChanged(object sender, EventArgs e)
    {
      LoadWithID();
    }

    #endregion
  }
}
